#Region "Using Directives"
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports C4F.DevKit.PowerManagement
#End Region
Namespace PowerManagementApplication
	Public Partial Class frmPowerManagement
	Inherits Form
		#Region "Global Data"
		Private powerManager As PowerManager = New PowerManager()
		Private schemeValue As PowerScheme = Nothing
		Private toolTip As ToolTip = New ToolTip()
		#End Region
		#Region "Methods"
		#Region "Tab Initialize methods"
		''' <summary>
		''' Constructor. Keeps GUI in particular state at start.
		''' </summary>
		Public Sub New()
			InitializeComponent()
			cmbModes.SelectedIndex = 0
			InitializeWindowsSecurityTab()
			cbxSetTimer.Enabled = False
			cbxOptionForce.Enabled = False
		End Sub
		''' <summary>
		''' Keeps controls of Tab-page 'Windows Security' in initial state.
		''' </summary>
		Private Sub InitializeWindowsSecurityTab()
			cbxOptionForce.Enabled = True
			cbxOptionForce.Checked = False
			cbxDisableWakeEvent.Enabled = False
			cbxDisableWakeEvent.Checked = False
			cbxSetTimer.Enabled = True
			cbxSetTimer.Checked = False
			gbxTimerSettings.Enabled = False
		End Sub
		''' <summary>
		''' Keeps controls of Tab-page 'Power Schemes'in initial state.
		''' </summary>
		Private Sub InitializePowerSchemeTab()
			btnRefresh.Enabled = True
			btnApply.Enabled = False
			btnDelete.Enabled = False
		End Sub
		''' <summary>
		''' Keeps controls of Tab-page 'Battery Status'in initial state.
		''' </summary>
		Private Sub InitializeBatteryStatusTab()
			tbxBatteryChargeStatus.Text = String.Empty
			tbxBatteryLife.Text = String.Empty
			tbxIsCharging.Text = String.Empty
		End Sub
		''' <summary>
		''' Keeps label controls of Tab-page 'Battery Status' according to state parameter
		''' passed.
		''' </summary>
		''' <param name="state"></param>
		Private Sub InitializeBatteryStatusLabels(ByVal state As Boolean)
			lblBatteryStatus.Enabled = state
			lblIsCharging.Enabled = state
			lblBatteryLife.Enabled = state
			lblBatteryChargeState.Enabled = state
			progressBarBatteryLife.Enabled = state
			lblBatteryLifePercentage.Enabled = state
		End Sub
		#End Region
		#Region "Windows Secuirity Methods"
		''' <summary>
		''' Shutdowns machine based on force and timer parameters. 
		''' </summary>
		Private Sub Shutdown()
			Try
				Dim force As Boolean = cbxOptionForce.Checked
				Dim shutdownMessage As String = tbxMessage.Text
                Dim secondsTimer As UInt32
				If cbxSetTimer.Checked Then
					Try
						secondsTimer = Convert.ToUInt32(tbxTimeSeconds.Text)
					Catch
						MessageBox.Show("Please specify positive integer value.", "Power Management", MessageBoxButtons.OK, MessageBoxIcon.Information)
						Return
					End Try
					powerManager.ShutdownComputer(shutdownMessage, secondsTimer, force)
				Else
					powerManager.ShutdownComputer(force)
				End If
			Catch ex As PowerManagerException
				MessageBox.Show(ex.Message, "Power Management", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			Catch ex As ArgumentException
				MessageBox.Show(ex.Message, "Power Management", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
			End Try
		End Sub
		''' <summary>
		''' Reboots machine based on force and timer parameters. 
		''' </summary>
		Private Sub Reboot()
			Try
				Dim force As Boolean = cbxOptionForce.Checked
				Dim shutdownMessage As String = tbxMessage.Text
                Dim secondsTimer As UInt32
				If cbxSetTimer.Checked Then
					Try
                        secondsTimer = Convert.ToUInt32(tbxTimeSeconds.Text)
					Catch
						MessageBox.Show("Please specify positive integer value.", "Power Management", MessageBoxButtons.OK, MessageBoxIcon.Information)
						Return
					End Try
					powerManager.RebootComputer(shutdownMessage, secondsTimer, force)
				Else
					powerManager.RebootComputer(force)
				End If
			Catch ex As PowerManagerException
				MessageBox.Show(ex.Message, "Power Management", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			Catch ex As ArgumentException
				MessageBox.Show(ex.Message, "Power Management", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
			End Try
		End Sub
		''' <summary>
		''' Logs off current with\without force.
		''' </summary>
		Private Sub LogOff()
			Try
				Dim force As Boolean = cbxOptionForce.Checked
				powerManager.LogOffCurrentUser(force)
			Catch ex As PowerManagerException
				MessageBox.Show(ex.Message, "Power Management", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			End Try
		End Sub
		''' <summary>
		''' Poweroff the computer
		''' </summary>
		Private Sub PowerOff()
			Try
				Dim force As Boolean = cbxOptionForce.Checked
				powerManager.PowerOffComputer(force)
			Catch ex As PowerManagerException
				MessageBox.Show(ex.Message, "Power Management", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			End Try
		End Sub
		''' <summary>
		''' Puts computer in hibernate state.
		''' </summary>
		Private Sub Hibernate()
			Try
				Dim force As Boolean = cbxOptionForce.Checked
				powerManager.HibernateComputer(force)
			Catch ex As PowerManagerException
				MessageBox.Show(ex.Message, "Power Management", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			Catch ex As InvalidOperationException
				MessageBox.Show(ex.Message, "Power Management", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			End Try
		End Sub
		''' <summary>
		''' Puts computer in Standby state.
		''' </summary>
		Private Sub StandByComputer()
			Try
				Dim force As Boolean = cbxOptionForce.Checked
				Dim disableWakeEvent As Boolean = cbxDisableWakeEvent.Checked
				powerManager.StandbyComputer(force, disableWakeEvent)
			Catch ex As PowerManagerException
				MessageBox.Show(ex.Message, "Power Management", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			End Try
		End Sub
		''' <summary>
		''' Puts workstation in locked state.
		''' </summary>
		Private Sub LockWorkStation()
			Try
				powerManager.LockWorkStation()
			Catch ex As PowerManagerException
				MessageBox.Show(ex.Message, "Power Management", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			End Try
		End Sub
		#End Region
		#Region "Power schemes Methods"
		''' <summary>
		''' Shows active power scheme in 'Active Power Scheme' textbox.
		''' </summary>
		Private Sub ShowActivePowerScheme()
			Try
				tbxActivePowerScheme.Text = powerManager.GetActivePowerScheme()
			Catch ex As PowerManagerException
				MessageBox.Show(ex.Message, "Power Management", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			End Try
		End Sub
		''' <summary>
		''' Shows all available power schemes in listbox provided.
		''' </summary>
		Private Sub ShowAvailablePowerScheme()
			Try
				lbxAvailablePowerSchemes.Items.Clear()
				Dim powerSchemes As List(Of String) = powerManager.GetAvailablePowerSchemes()
				For Each scheme As String In powerSchemes
					lbxAvailablePowerSchemes.Items.Add(scheme)
				Next
			Catch ex As PowerManagerException
				MessageBox.Show(ex.Message, "Power Management", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			End Try
		End Sub
		#End Region
		#Region "BatteryStatus Methods"
		''' <summary>
		''' This function contains code to show battery details on 
		''' Tab-page 'Battery Status'
		''' </summary>
		''' <param name="batteryStatus"></param>
		Private Sub RenderBatteryValues(ByVal batteryStatus As BatteryStatus)
			Dim flag As BatteryFlag = batteryStatus.BatteryFlag And Not BatteryFlag.Charging
			tbxBatteryChargeStatus.Text = flag.ToString()
			tbxBatteryLife.Text = batteryStatus.BatteryLifePercent.ToString() + "%"
			tbxIsCharging.Text = batteryStatus.Charging.ToString()
			If batteryStatus.BatteryPresent Then
				progressBarBatteryLife.Value = batteryStatus.BatteryLifePercent
				lblBatteryLifePercentage.Text = batteryStatus.BatteryLifePercent.ToString() + "%"
			End If
		End Sub
		''' <summary>
		''' Function checks whether battery present in machine or not.
		''' If yes renders battery attributes in textboxes provided on page.
		''' </summary>
		Private Sub CheckForBattery()
			Try
				Dim batteryStatus As BatteryStatus = powerManager.GetBatteryStatus()
				If batteryStatus.BatteryPresent Then
					lblBatteryMessage.Text = String.Empty
					RenderBatteryValues(batteryStatus)
					InitializeBatteryStatusLabels(True)
				Else
					lblBatteryMessage.ForeColor = Color.Red
					lblBatteryMessage.Text = "Battery not found."
					InitializeBatteryStatusTab()
					InitializeBatteryStatusLabels(False)
				End If
			Catch ex As PowerManagerException
				MessageBox.Show(ex.Message, "Power Management", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			End Try
		End Sub
		#End Region
		#End Region
		#Region "Events"
		''' <summary>
		''' Initializes the tooltip object.
		''' </summary>
		''' <param name="sender"></param>
		''' <param name="e"></param>
        Private Sub frmPowerManagement_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
            toolTip.AutoPopDelay = 5000
            toolTip.InitialDelay = 1
            toolTip.ReshowDelay = 1
            toolTip.ShowAlways = True
        End Sub
		#Region "Tab Control Events"
		''' <summary>
		''' This function contains code to keeping controls in particular state 
		''' while navigating through tabs.
		''' </summary>
		''' <param name="sender"></param>
		''' <param name="e"></param>
        Private Sub tabctrlPowerManagement_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles tabctrlPowerManagement.SelectedIndexChanged
            Dim selectedIndex As Integer = tabctrlPowerManagement.SelectedIndex
            Select Case selectedIndex
                Case 0
                    DisableTimer()
                    Exit Select
                Case 1
                    InitializePowerSchemeTab()
                    ShowActivePowerScheme()
                    ShowAvailablePowerScheme()
                    DisableTimer()
                    Exit Select
                Case 2
                    InitializeBatteryStatusTab()
                    CheckForBattery()
                    EnableTimer()
                    Exit Select
                Case 3
                    InitializePowerSettingTab()
                    DisableTimer()
                    Exit Select
            End Select
        End Sub
		''' <summary>
		''' This function contains code to exit application.
		''' </summary>
		''' <param name="sender"></param>
		''' <param name="e"></param>
        Private Sub btmClose_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btmClose.Click
            Application.[Exit]()
        End Sub
		#End Region
		#Region "Windows Secuirity Events"
		''' <summary>
		''' This function changes the other controls state of 'Windows Security' 
		''' Tab-page as per operation selected in combobox.
		''' </summary>
		''' <param name="sender"></param>
		''' <param name="e"></param>
        Private Sub cmbModes_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles cmbModes.SelectedIndexChanged
            Dim selectedIndex As Integer = cmbModes.SelectedIndex
            Select Case selectedIndex
                Case 1
                    'shutdown
                    InitializeWindowsSecurityTab()
                    Exit Select
                Case 2
                    'Restart
                    InitializeWindowsSecurityTab()
                    Exit Select
                Case 3
                    'Log Off
                    InitializeWindowsSecurityTab()
                    cbxSetTimer.Enabled = False
                    Exit Select
                Case 4
                    'Hibernate
                    InitializeWindowsSecurityTab()
                    cbxSetTimer.Enabled = False
                    Exit Select
                Case 5
                    'PowerOff
                    InitializeWindowsSecurityTab()
                    cbxSetTimer.Enabled = False
                    Exit Select
                Case 6
                    'Stand By
                    InitializeWindowsSecurityTab()
                    cbxSetTimer.Enabled = False
                    cbxDisableWakeEvent.Enabled = True
                    Exit Select
                Case 0
                    'Lock System
                    InitializeWindowsSecurityTab()
                    cbxSetTimer.Enabled = False
                    cbxOptionForce.Enabled = False
                    Exit Select
            End Select
        End Sub
		''' <summary>
		''' This function decides 'TimeSettings' groupbox's enable state on 
		''' 'SetTimer' checkbox's check state.
		''' </summary>
		''' <param name="sender"></param>
		''' <param name="e"></param>
        Private Sub cbxSetTimer_CheckStateChanged(ByVal sender As Object, ByVal e As EventArgs) Handles cbxSetTimer.CheckStateChanged
            If cbxSetTimer.CheckState = CheckState.Checked Then
                gbxTimerSettings.Enabled = True
            Else
                gbxTimerSettings.Enabled = False
            End If
        End Sub
		''' <summary>
		''' This function clears 'TimeSeconds' and 'Message' textboxes if 'TimeSettings'
		''' groupbox turns to disable state.
		''' </summary>
		''' <param name="sender"></param>
		''' <param name="e"></param>
        Private Sub gbxTimerSettings_EnabledChanged(ByVal sender As Object, ByVal e As EventArgs) Handles gbxTimerSettings.EnabledChanged
            If gbxTimerSettings.Enabled = False Then
                tbxTimeSeconds.Text = String.Empty
                tbxMessage.Text = String.Empty
            End If
        End Sub
		''' <summary>
		''' This function covers functionality to perform action as per operation 
		''' selected in comboBox.
		''' </summary>
		''' <param name="sender"></param>
		''' <param name="e"></param>
        Private Sub btnOk_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnOk.Click
            Dim selectedIndex As Integer = cmbModes.SelectedIndex
            ' No confirmation for locking workstation.
            If selectedIndex > 0 Then
                If MessageBox.Show("Are you sure?", "Power Management", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Cancel Then
                    Return
                End If
            End If
            Select Case selectedIndex
                Case 1
                    'shutdown
                    Shutdown()
                    Exit Select
                Case 2
                    'Restart
                    Reboot()
                    Exit Select
                Case 3
                    'Log Off
                    LogOff()
                    Exit Select
                Case 4
                    'Hibernate
                    Hibernate()
                    Exit Select
                Case 5
                    'PowerOff
                    PowerOff()
                    Exit Select
                Case 6
                    'Stand By
                    StandByComputer()
                    Exit Select
                Case 0
                    'Lock System
                    LockWorkStation()
                    Exit Select
            End Select
        End Sub
		#End Region
		#Region "Power Schemes Events"
		''' <summary>
		''' Functionality to enable 'Apply' and 'Delete' buttons 
		''' when item from listbox is selected.
		''' </summary>
		''' <param name="sender"></param>
		''' <param name="e"></param>
        Private Sub lbxAvailablePowerSchemes_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles lbxAvailablePowerSchemes.SelectedIndexChanged
            btnApply.Enabled = True
            btnDelete.Enabled = True
        End Sub
		''' <summary>
		''' Functionality to refresh power scheme related data on Tab-page.
		''' </summary>
		''' <param name="sender"></param>
		''' <param name="e"></param>
        Private Sub btnRefresh_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnRefresh.Click
            ShowActivePowerScheme()
            ShowAvailablePowerScheme()
        End Sub
		''' <summary>
		''' Functionality to apply the selected powerscheme as active power scheme.
		''' </summary>
		''' <param name="sender"></param>
		''' <param name="e"></param>
        Private Sub btnApply_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnApply.Click
            Try
                If MessageBox.Show("Are you sure?", "Power Management", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = Windows.Forms.DialogResult.OK Then
                    powerManager.SetActivePowerScheme(lbxAvailablePowerSchemes.Text)
                Else
                    Return
                End If
            Catch ex As PowerManagerException
                MessageBox.Show(ex.Message, "Power Management", MessageBoxButtons.OK, MessageBoxIcon.[Error])
            Catch ex As ArgumentNullException
                MessageBox.Show("Please Select Power scheme", "Power Management", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Catch ex As ArgumentException
                MessageBox.Show(ex.Message, "Power Management", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End Try
            ShowActivePowerScheme()
        End Sub
		''' <summary>
		''' Functionality to delete any power scheme.
		''' </summary>
		''' <param name="sender"></param>
		''' <param name="e"></param>
        Private Sub btnDelete_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnDelete.Click
            Try
                If MessageBox.Show("Are you sure?", "Power Management", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = Windows.Forms.DialogResult.OK Then
                    powerManager.DeletePowerScheme(lbxAvailablePowerSchemes.Text)
                Else
                    Return
                End If
            Catch ex As PowerManagerException
                MessageBox.Show(ex.Message, "Power Management", MessageBoxButtons.OK, MessageBoxIcon.[Error])
            Catch ex As ArgumentNullException
                MessageBox.Show("Please Select Power scheme", "Power Management", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Catch ex As ArgumentException
                MessageBox.Show(ex.Message, "Power Management", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End Try
            ShowAvailablePowerScheme()
        End Sub
		#End Region
		''' <summary>
		''' When called it initializes the battery status tab.
		''' </summary>
		''' <param name="sender"></param>
		''' <param name="e"></param>
        Private Sub timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles tmrRenderValues.Tick
            InitializeBatteryStatusTab()
            CheckForBattery()
        End Sub
		''' <summary>
		''' Enables the timer.
		''' </summary>
		Private Sub EnableTimer()
			tmrRenderValues.Enabled = True
			tmrRenderValues.Start()
		End Sub
		''' <summary>
		''' Disables the timer.
		''' </summary>
		Private Sub DisableTimer()
			tmrRenderValues.[Stop]()
			tmrRenderValues.Enabled = False
		End Sub
		#End Region
		Private Sub DisableComboBox(ByVal cmbBox As ComboBox)
			cmbBox.Items.Clear()
			cmbBox.Enabled = False
		End Sub
		''' <summary>
		''' Keeps controls of Tab-page 'Power Setting'in initial state.
		''' </summary>
		Private Sub InitializePowerSettingTab()
			' Populate the combo box
			Try
				btnGetSettings.Enabled = False
				' Get the active scheme
				Dim activeScheme As String = powerManager.GetActivePowerScheme()
				cmbSchemes.Items.Clear()
				ClearPowerSettingTextBoxes()
				DisableComboBox(cmbSubGroup)
				DisableComboBox(cmbSetting)
				Dim powerSchemes As List(Of String) = powerManager.GetAvailablePowerSchemes()
				For Each scheme As String In powerSchemes
					cmbSchemes.Items.Add(scheme)
				Next
			Catch ex As PowerManagerException
				MessageBox.Show(ex.Message, "Power Management", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			End Try
		End Sub
		''' <summary>
		''' Initializes the power setting list and clear the text boxes.
		''' </summary>
		''' <param name="sender"></param>
		''' <param name="e"></param>
        Private Sub cmbSubGroup_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles cmbSubGroup.SelectedIndexChanged
            InitializeSettingsList()
            ClearPowerSettingTextBoxes()
        End Sub
		''' <summary>
		''' Initializes the sub group list, clear the power setting list and the text boxes.
		''' </summary>
		''' <param name="sender"></param>
		''' <param name="e"></param>
        Private Sub cmbSchemes_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles cmbSchemes.SelectedIndexChanged
            InitializeSubGroupList()
            cmbSetting.Items.Clear()
            ClearPowerSettingTextBoxes()
        End Sub
		''' <summary>
		''' Initializes the sub group list.
		''' </summary>
		Private Sub InitializeSubGroupList()
			Try
                Windows.Forms.Cursor.Current = Cursors.WaitCursor
				cmbSubGroup.Enabled = True
				Dim activeScheme As String = powerManager.GetActivePowerScheme()
				If rdoAcSettings.Checked Then
					schemeValue = powerManager.ReadPowerSchemeACValue(activeScheme)
				Else
					schemeValue = powerManager.ReadPowerSchemeDCValue(activeScheme)
				End If
				cmbSubGroup.Items.Clear()
				For Each subgroup As SettingSubGroup In schemeValue.SubGroups
					cmbSubGroup.Items.Add(subgroup.Name)
				Next
                Windows.Forms.Cursor.Current = Cursors.[Default]
			Catch ex As PowerManagerException
                Windows.Forms.Cursor.Current = Cursors.[Default]
				MessageBox.Show(ex.Message, "Power Management", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			Catch ex As ArgumentNullException
                Windows.Forms.Cursor.Current = Cursors.[Default]
				MessageBox.Show(ex.Message, "Power Management", MessageBoxButtons.OK, MessageBoxIcon.Information)
			Catch ex As ArgumentException
                Windows.Forms.Cursor.Current = Cursors.[Default]
				MessageBox.Show(ex.Message, "Power Management", MessageBoxButtons.OK, MessageBoxIcon.Information)
			End Try
		End Sub
		''' <summary>
		''' Initializes the power setting list.
		''' </summary>
		Private Sub InitializeSettingsList()
			Try
                Windows.Forms.Cursor.Current = Cursors.WaitCursor
				cmbSetting.Enabled = True
				btnGetSettings.Enabled = False
				If rdoAcSettings.Checked Then
					schemeValue = powerManager.ReadPowerSchemeACValue(cmbSchemes.Text)
				Else
					schemeValue = powerManager.ReadPowerSchemeDCValue(cmbSchemes.Text)
				End If
				For Each subgroup As SettingSubGroup In schemeValue.SubGroups
					If [String].Equals(subgroup.Name, cmbSubGroup.Text) Then
						cmbSetting.Items.Clear()
						For Each setting As PowerSetting In subgroup.PowerSettings
							cmbSetting.Items.Add(setting.Name)
						Next
						Exit For
					End If
				Next
                Windows.Forms.Cursor.Current = Cursors.[Default]
			Catch ex As PowerManagerException
                Windows.Forms.Cursor.Current = Cursors.[Default]
				MessageBox.Show(ex.Message, "Power Management", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			Catch ex As ArgumentNullException
                Windows.Forms.Cursor.Current = Cursors.[Default]
				MessageBox.Show(ex.Message, "Power Management", MessageBoxButtons.OK, MessageBoxIcon.Information)
			Catch ex As ArgumentException
                Windows.Forms.Cursor.Current = Cursors.[Default]
				MessageBox.Show(ex.Message, "Power Management", MessageBoxButtons.OK, MessageBoxIcon.Information)
			End Try
		End Sub
		''' <summary>
		''' Clear the text boxes and the tooltips.
		''' </summary>
		Private Sub ClearPowerSettingTextBoxes()
			tbxSelectedScheme.Text = [String].Empty
			tbxSelectedSubGroup.Text = [String].Empty
			tbxSelectedSetting.Text = [String].Empty
			tbxSettingValue.Text = [String].Empty
			btnGetSettings.Enabled = False
			toolTip.RemoveAll()
		End Sub
		''' <summary>
		''' Gets the setting values and initializes the tooltip.
		''' </summary>
		''' <param name="sender"></param>
		''' <param name="e"></param>
        Private Sub btnGetSettings_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnGetSettings.Click
            Windows.Forms.Cursor.Current = Cursors.WaitCursor
            Try
                If rdoAcSettings.Checked Then
                    schemeValue = powerManager.ReadPowerSchemeACValue(cmbSchemes.Text)
                Else
                    schemeValue = powerManager.ReadPowerSchemeDCValue(cmbSchemes.Text)
                End If
                For Each subgroup As SettingSubGroup In schemeValue.SubGroups
                    If [String].Equals(subgroup.Name, cmbSubGroup.Text) Then
                        For Each setting As PowerSetting In subgroup.PowerSettings
                            If [String].Equals(setting.Name, cmbSetting.Text) Then
                                tbxSelectedScheme.Text = cmbSchemes.Text
                                tbxSelectedSubGroup.Text = cmbSubGroup.Text
                                tbxSelectedSetting.Text = cmbSetting.Text
                                tbxSettingValue.Text = setting.Value
                                toolTip.SetToolTip(Me.tbxSelectedScheme, schemeValue.Description)
                                toolTip.SetToolTip(Me.tbxSelectedSubGroup, subgroup.Description)
                                toolTip.SetToolTip(Me.tbxSelectedSetting, setting.Description)
                                Exit For
                            End If
                        Next
                        Exit For
                    End If
                Next
                Windows.Forms.Cursor.Current = Cursors.[Default]
            Catch ex As PowerManagerException
                Windows.Forms.Cursor.Current = Cursors.[Default]
                MessageBox.Show(ex.Message, "Power Management", MessageBoxButtons.OK, MessageBoxIcon.[Error])
            Catch ex As ArgumentNullException
                Windows.Forms.Cursor.Current = Cursors.[Default]
                MessageBox.Show(ex.Message, "Power Management", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Catch ex As ArgumentException
                Windows.Forms.Cursor.Current = Cursors.[Default]
                MessageBox.Show(ex.Message, "Power Management", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End Try
        End Sub
		''' <summary>
		''' Clear the text boxes and activates the button.
		''' </summary>
		''' <param name="sender"></param>
		''' <param name="e"></param>
        Private Sub cmbSetting_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles cmbSetting.SelectedIndexChanged
            ClearPowerSettingTextBoxes()
            btnGetSettings.Enabled = True
        End Sub
		''' <summary>
		''' Clear the text boxes.
		''' </summary>
		''' <param name="sender"></param>
		''' <param name="e"></param>
        Private Sub rdoAcSettings_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles rdoAcSettings.CheckedChanged
            Dim buttonEnabled As Boolean = False
            If btnGetSettings.Enabled = True Then
                buttonEnabled = True
            End If
            ClearPowerSettingTextBoxes()
            If buttonEnabled = True Then
                btnGetSettings.Enabled = True
            End If
        End Sub
    End Class
End Namespace
